﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;

namespace ConAppCore.Test
{
    internal class TestFileEx
    {
        public static void Test()
        {
            //TestWrite();
            TestRead();
            TestReadWrite();
        }

        private static void TestReadWrite()
        {
            try
            {
                string filePath = @"D:\datas\BPSK-43EBN0-1200kbd-6400KHzFs.dat";
                using var stream1 = new FileStream(filePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
                using var stream2 = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                //using var stream = new FileStream(@"D:\Projects\212\212协议.docx", FileMode.Open, FileAccess.Read, FileShare.Read);
                //using var stream = new FileStream(@"D:\Projects\212\212协议2223.docx", FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (IOException ioex)
            {
                string st = Marshal.GetExceptionForHR(ioex.HResult).Message;
                if (ioex.HResult == -2147024864)
                {
                    //-2147024864   文件被其它程序打开
                    //-2147024894  文件不存在
                }
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestRead()
        {
            string filePath = @"D:\Temp\iq.dat";

            using var iqPointer = new IQUnmanagedMemoryPointer<double>(10);
            using var complexPointer = new UnmanagedMemoryPointer<Complex>(10);
            var len1 = FileEx.ReadIQ(filePath, 0, TypeCode.Int16, 10);
            var len2 = FileEx.ReadIQ(filePath, 0, TypeCode.Int16, complexPointer.GetSpan());
            var len3 = FileEx.ReadIQ(filePath, 0, TypeCode.Int16, complexPointer.GetSpanZ());
            var len4 = FileEx.ReadIQ(filePath, 0, TypeCode.Int16, iqPointer.GetIQSpanZ());



            byte[] buffer1 = FileEx.Read(filePath, 0, 10);
            byte[] buffer = new byte[10];
            var len5 = FileEx.Read(filePath, 0, buffer, 0, 10);
            using var spanPointer = new UnmanagedMemoryPointer<byte>(10);
            var len6 = FileEx.Read(filePath, 0, spanPointer.GetSpan());
            var len7 = FileEx.Read(filePath, 0, spanPointer.GetSpanZ());

            using var span2Pointer = new UnmanagedMemoryPointer<short>(10);
            var len8 = FileEx.Read(filePath, 0, span2Pointer.GetSpanZ());
            var len9 = FileEx.Read(filePath, 0, span2Pointer.GetSpanZ());
        }
        private static void TestWrite()
        {
            using var iqPointer = new IQUnmanagedMemoryPointer<short>(10);
            using var complexPointer = new UnmanagedMemoryPointer<Complex>(10);
            byte[] buffer = new byte[10];
            using var spanPointer = new UnmanagedMemoryPointer<int>(10);

            for (int i = 0; i < iqPointer.Length; i++)
            {
                iqPointer.SetI(i, (short)i);
                iqPointer.SetQ(i, (short)i);
                complexPointer[i] = new Complex(i, i);
                buffer[i] = (byte)i;
                spanPointer[i] = i;
            }
            FileEx.Write(iqPointer.GetIQSpanZ(), @"D:\Temp\iq.dat");
            FileEx.Write(complexPointer.GetSpan(), @"D:\Temp\complex.dat");
            FileEx.Write(complexPointer.GetSpanZ(), @"D:\Temp\complexZ.dat");
            FileEx.Write(buffer, @"D:\Temp\buffer.dat");
            FileEx.Write(buffer, 0, buffer.Length, @"D:\Temp\buffer2.dat");
            FileEx.Write(spanPointer.GetSpan(), @"D:\Temp\span.dat");
            FileEx.Write(spanPointer.GetSpanZ(), @"D:\Temp\spanZ.dat");

            FileEx.Append(iqPointer.GetIQSpanZ(), @"D:\Temp\iq.dat");
            FileEx.Append(complexPointer.GetSpan(), @"D:\Temp\complex.dat");
            FileEx.Append(complexPointer.GetSpanZ(), @"D:\Temp\complexZ.dat");
            FileEx.Append(buffer, @"D:\Temp\buffer.dat");
            FileEx.Append(buffer, 0, buffer.Length, @"D:\Temp\buffer2.dat");
            FileEx.Append(spanPointer.GetSpan(), @"D:\Temp\span.dat");
            FileEx.Append(spanPointer.GetSpanZ(), @"D:\Temp\spanZ.dat");
        }

    }
}
